/*++

Copyright (c) 2007  Microsoft Corporation 

Filename: 

    NRTMMenu.cs

Abstract: 

    A class to create the NRTM Menu
*/
using System;
using System.Collections.Generic;
using System.Text;
using Visio = Microsoft.Office.Interop.Visio;
using Office = Microsoft.Office.Core;


namespace NRTMAddIn
{
    /// <summary>
    /// Class to load and unload NRTM menu
    /// </summary>
    class NRTMMenu
    {
        #region Private Variables

        // privates
        private Visio.UIObject uiObject;
        private Visio.Menu nrtmMenu;
        private Visio.MenuItem nrtmMenuItemSelect;
        private Visio.MenuItem nrtmMenuItemPerformMonitoring;
        private Visio.MenuItem nrtmMenuItemStartMonitoring;
        private Visio.MenuItem nrtmMenuItemStopMonitoring;
        private Visio.MenuItem nrtmMenuItemShowStatusItem;
        private Visio.MenuItem nrtmMenuItemShowReportItem;
        private Visio.MenuItem nrtmMenuItemExportExcelItem;
        private Visio.MenuItem nrtmMenuItemOptions;
        private Visio.MenuItem nrtmMenuItemHelp;
        private Visio.MenuItem nrtmMenuItemAbout;

        #endregion

        #region Methods

        /// <summary>
        /// Function to add the NRTM monitor menu to Visio Document
        /// </summary>
        public void BuildMenu(Document nrtmDocument)
        {
            System.Text.StringBuilder addonArgs = null;

            try
            {
                //Get the custom menu or built in menus to the UIObject
                if (nrtmDocument.VisioDocument.CustomMenus != null)
                {
                    uiObject = nrtmDocument.VisioDocument.CustomMenus.Clone;
                }
                else if (nrtmDocument.VisioDocument.Application.CustomMenus != null)
                {
                    uiObject = nrtmDocument.VisioDocument.Application.CustomMenus.Clone;
                }
                else
                {
                    uiObject = nrtmDocument.VisioDocument.Application.BuiltInMenus.Clone;
                }
                if (uiObject == null)
                {
                    //Menu add failed
                    return;
                }
                //Get the menu sets of the visio application
                Visio.MenuSets menuSets = uiObject.MenuSets;
                //Get the item Id of the drawing object
                short itemId = (short)Visio.VisUIObjSets.visUIObjSetDrawing;
                //Get the menu set of drawing object
                Visio.MenuSet menuSet = menuSets.get_ItemAtID(itemId);
                //Get the menus from menu set
                Visio.Menus menus = menuSet.Menus;

                //Add the menu at a position & set the caption
                nrtmMenu = menus.AddAt(menus.Count - 1);
                nrtmMenu.Caption = Global.GetResourceString("NRTM_Menu_Title"); 

                //Set the menu items to the menu
                Visio.MenuItems menuItems = nrtmMenu.MenuItems;

                //Add the select network machine menu Item to the menu
                nrtmMenuItemSelect = menuItems.Add();
                nrtmMenuItemSelect.Caption = Global.GetResourceString("Select_Machines_MenuItem_Title"); 
                addonArgs = new System.Text.StringBuilder();
                addonArgs.Append(Global.DocumentContextString);
                addonArgs.Append(nrtmDocument.VisioDocument.ID);
                addonArgs.Append(" ");
                addonArgs.Append(Global.ApplicationContextString);
                addonArgs.Append(Global.ApplicationID);
                addonArgs.Append(" ");
                addonArgs.Append(Global.CommandContextString);
                addonArgs.Append(Global.CommandStringSelectMachines);
                nrtmMenuItemSelect.AddOnName = "QueueMarkerEvent";
                nrtmMenuItemSelect.AddOnArgs = addonArgs.ToString();

                //Add the perform monitoring menu Item to the menu
                nrtmMenuItemPerformMonitoring = menuItems.Add();
                nrtmMenuItemPerformMonitoring.Caption = Global.GetResourceString("Perform_Monitoring_MenuItem_Title"); 
                nrtmMenuItemPerformMonitoring.BeginGroup = true;

                //Add Start & Stop sub menu item
                Visio.MenuItems nrtmItems = nrtmMenuItemPerformMonitoring.MenuItems;
                nrtmMenuItemStartMonitoring = nrtmItems.Add();
                nrtmMenuItemStartMonitoring.Caption = Global.GetResourceString("Start_MenuItem_Title"); 
                addonArgs = new System.Text.StringBuilder();
                addonArgs.Append(Global.DocumentContextString);
                addonArgs.Append(nrtmDocument.VisioDocument.ID);
                addonArgs.Append(" ");
                addonArgs.Append(Global.ApplicationContextString);
                addonArgs.Append(Global.ApplicationID);
                addonArgs.Append(" ");
                addonArgs.Append(Global.CommandContextString);
                addonArgs.Append(Global.CommandStringStartMonitoring);
                nrtmMenuItemStartMonitoring.AddOnName = "QueueMarkerEvent";
                nrtmMenuItemStartMonitoring.AddOnArgs = addonArgs.ToString();

                nrtmMenuItemStopMonitoring = nrtmItems.Add();
                nrtmMenuItemStopMonitoring.Caption = Global.GetResourceString("Stop_MenuItem_Title");
                addonArgs = new System.Text.StringBuilder();
                addonArgs.Append(Global.DocumentContextString);
                addonArgs.Append(nrtmDocument.VisioDocument.ID);
                addonArgs.Append(" ");
                addonArgs.Append(Global.ApplicationContextString);
                addonArgs.Append(Global.ApplicationID);
                addonArgs.Append(" ");
                addonArgs.Append(Global.CommandContextString);
                addonArgs.Append(Global.CommandStringStopMonitoring);
                nrtmMenuItemStopMonitoring.AddOnName = "QueueMarkerEvent";
                nrtmMenuItemStopMonitoring.AddOnArgs = addonArgs.ToString();

                //Add the status menu Item to the menu
                nrtmMenuItemShowStatusItem = menuItems.Add();
                nrtmMenuItemShowStatusItem.Caption = Global.GetResourceString("Status_Window_Title");
                nrtmMenuItemShowStatusItem.State = (short)Visio.VisUIButtonState.visButtonUp;
                nrtmMenuItemShowStatusItem.BeginGroup = true;
                addonArgs = new System.Text.StringBuilder();
                addonArgs.Append(Global.DocumentContextString);
                addonArgs.Append(nrtmDocument.VisioDocument.ID);
                addonArgs.Append(" ");
                addonArgs.Append(Global.ApplicationContextString);
                addonArgs.Append(Global.ApplicationID);
                addonArgs.Append(" ");
                addonArgs.Append(Global.CommandContextString);
                addonArgs.Append(Global.CommandStringShowStatusWindow);
                nrtmMenuItemShowStatusItem.AddOnName = "QueueMarkerEvent";
                nrtmMenuItemShowStatusItem.AddOnArgs = addonArgs.ToString();

                //Add the report menu Item to the menu
                nrtmMenuItemShowReportItem = menuItems.Add();
                nrtmMenuItemShowReportItem.Caption = Global.GetResourceString("Report_Window_Title");
                nrtmMenuItemShowReportItem.State = (short)Visio.VisUIButtonState.visButtonUp;
                addonArgs = new System.Text.StringBuilder();
                addonArgs.Append(Global.DocumentContextString);
                addonArgs.Append(nrtmDocument.VisioDocument.ID);
                addonArgs.Append(" ");
                addonArgs.Append(Global.ApplicationContextString);
                addonArgs.Append(Global.ApplicationID);
                addonArgs.Append(" ");
                addonArgs.Append(Global.CommandContextString);
                addonArgs.Append(Global.CommandStringShowReportWindow);
                nrtmMenuItemShowReportItem.AddOnName = "QueueMarkerEvent";
                nrtmMenuItemShowReportItem.AddOnArgs = addonArgs.ToString();

                //Add the export to excel menu Item to the menu
                nrtmMenuItemExportExcelItem = menuItems.Add();
                nrtmMenuItemExportExcelItem.Caption = Global.GetResourceString("ExportToExcel_Title");
                addonArgs = new System.Text.StringBuilder();
                addonArgs.Append(Global.DocumentContextString);
                addonArgs.Append(nrtmDocument.VisioDocument.ID);
                addonArgs.Append(" ");
                addonArgs.Append(Global.ApplicationContextString);
                addonArgs.Append(Global.ApplicationID);
                addonArgs.Append(" ");
                addonArgs.Append(Global.CommandContextString);
                addonArgs.Append(Global.CommandStringExportReportToExcel);
                nrtmMenuItemExportExcelItem.AddOnName = "QueueMarkerEvent";
                nrtmMenuItemExportExcelItem.AddOnArgs = addonArgs.ToString();
                nrtmMenuItemExportExcelItem.BeginGroup = true;

                //Add the Options menu Item to the menu
                nrtmMenuItemOptions = menuItems.Add();
                nrtmMenuItemOptions.Caption = Global.GetResourceString("Options_MenuItem_Title");
                nrtmMenuItemOptions.BeginGroup = true;
                addonArgs = new System.Text.StringBuilder();
                addonArgs.Append(Global.DocumentContextString);
                addonArgs.Append(nrtmDocument.VisioDocument.ID);
                addonArgs.Append(" ");
                addonArgs.Append(Global.ApplicationContextString);
                addonArgs.Append(Global.ApplicationID);
                addonArgs.Append(" ");
                addonArgs.Append(Global.CommandContextString);
                addonArgs.Append(Global.CommandStringNRTMOptions);
                nrtmMenuItemOptions.AddOnName = "QueueMarkerEvent";
                nrtmMenuItemOptions.AddOnArgs = addonArgs.ToString();

                //Add the help menu Item to the menu
                nrtmMenuItemHelp = menuItems.Add();
                nrtmMenuItemHelp.Caption = Global.GetResourceString("Help_MenuItem_Title");
                addonArgs = new System.Text.StringBuilder();
                addonArgs.Append(Global.DocumentContextString);
                addonArgs.Append(nrtmDocument.VisioDocument.ID);
                addonArgs.Append(" ");
                addonArgs.Append(Global.ApplicationContextString);
                addonArgs.Append(Global.ApplicationID);
                addonArgs.Append(" ");
                addonArgs.Append(Global.CommandContextString);
                addonArgs.Append(Global.CommandStringHelp);
                nrtmMenuItemHelp.AddOnName = "QueueMarkerEvent";
                nrtmMenuItemHelp.AddOnArgs = addonArgs.ToString();
                nrtmMenuItemHelp.BeginGroup = true;
                nrtmMenuItemHelp.FaceID = 133;

                //Add the about menu Item to the menu
                nrtmMenuItemAbout = menuItems.Add();
                nrtmMenuItemAbout.Caption = Global.GetResourceString("MenuItem_Title_About");
                addonArgs = new System.Text.StringBuilder();
                addonArgs.Append(Global.DocumentContextString);
                addonArgs.Append(nrtmDocument.VisioDocument.ID);
                addonArgs.Append(" ");
                addonArgs.Append(Global.ApplicationContextString);
                addonArgs.Append(Global.ApplicationID);
                addonArgs.Append(" ");
                addonArgs.Append(Global.CommandContextString);
                addonArgs.Append(Global.CommandStringAbout);
                nrtmMenuItemAbout.AddOnName = "QueueMarkerEvent";
                nrtmMenuItemAbout.AddOnArgs = addonArgs.ToString();
                nrtmMenuItemAbout.BeginGroup = true;

                NRTMMenu_UIBuilder_RefreshUIObject(Global.MenuStateDocumentOpen);

                //Set the custom menu to the active document
                nrtmDocument.VisioDocument.SetCustomMenus(uiObject);
                uiObject.UpdateUI();
            }
            catch (Exception ex)
            {
                throw new NrtmException("NRTM build menu failed", ex);
            }
        }

        /// <summary>
        /// Update the menu status based on the state of the document
        /// </summary>
        /// <param name="menuState"></param>
        internal void NRTMMenu_UIBuilder_RefreshUIObject(string menuState)
        {
            switch (menuState)
            {
                case Global.MenuStateApplicationOpen:
                    nrtmMenuItemSelect.Enabled = true;
                    nrtmMenuItemPerformMonitoring.Enabled = false;
                    nrtmMenuItemStartMonitoring.Enabled = false;
                    nrtmMenuItemStopMonitoring.Enabled = false;
                    nrtmMenuItemShowStatusItem.Enabled = false;
                    nrtmMenuItemShowReportItem.Enabled = false;
                    nrtmMenuItemExportExcelItem.Enabled = false;
                    nrtmMenuItemOptions.Enabled = true;
                    nrtmMenuItemHelp.Enabled = true;
                    break;

                case Global.MenuStateDiagramCreation:
                    nrtmMenuItemSelect.Enabled = true;
                    nrtmMenuItemPerformMonitoring.Enabled = false;
                    nrtmMenuItemStartMonitoring.Enabled = false;
                    nrtmMenuItemStopMonitoring.Enabled = false;
                    nrtmMenuItemShowStatusItem.Enabled = true;
                    nrtmMenuItemShowStatusItem.State = (short)Visio.VisUIButtonState.visButtonDown;
                    nrtmMenuItemShowReportItem.Enabled = false;
                    nrtmMenuItemExportExcelItem.Enabled = false;
                    nrtmMenuItemOptions.Enabled = true;
                    nrtmMenuItemHelp.Enabled = true;
                    break;

                case Global.MenuStateDocumentOpen:
                case Global.MenuStateDiagramCreated:
                    nrtmMenuItemSelect.Enabled = true;
                    nrtmMenuItemPerformMonitoring.Enabled = true;
                    nrtmMenuItemStartMonitoring.Enabled = true;
                    nrtmMenuItemStopMonitoring.Enabled = false;
                    nrtmMenuItemShowReportItem.Enabled = false;
                    nrtmMenuItemExportExcelItem.Enabled = false;
                    nrtmMenuItemOptions.Enabled = true;
                    nrtmMenuItemHelp.Enabled = true;
                    break;

                case Global.MenuStateMonitoringInProgress:
                    nrtmMenuItemSelect.Enabled = true;
                    nrtmMenuItemPerformMonitoring.Enabled = true;
                    nrtmMenuItemStartMonitoring.Enabled = false;
                    nrtmMenuItemStopMonitoring.Enabled = true;
                    nrtmMenuItemShowStatusItem.Enabled = true;
                    nrtmMenuItemShowStatusItem.State = (short)Visio.VisUIButtonState.visButtonDown;
                    nrtmMenuItemShowReportItem.Enabled = false;
                    nrtmMenuItemShowReportItem.State = (short)Visio.VisUIButtonState.visButtonUp;
                    nrtmMenuItemExportExcelItem.Enabled = false;
                    nrtmMenuItemOptions.Enabled = true;
                    nrtmMenuItemHelp.Enabled = true;
                    break;

                case Global.MenuStateMonitoringCompleted:
                    nrtmMenuItemSelect.Enabled = true;
                    nrtmMenuItemPerformMonitoring.Enabled = true;
                    nrtmMenuItemStartMonitoring.Enabled = true;
                    nrtmMenuItemStopMonitoring.Enabled = false;
                    nrtmMenuItemShowStatusItem.Enabled = true;
                    nrtmMenuItemShowReportItem.Enabled = true;
                    nrtmMenuItemShowReportItem.State = (short)Visio.VisUIButtonState.visButtonDown;
                    nrtmMenuItemExportExcelItem.Enabled = true;
                    nrtmMenuItemOptions.Enabled = true;
                    nrtmMenuItemHelp.Enabled = true;
                    break;

                case Global.MenuStateSetStatus:
                    nrtmMenuItemShowStatusItem.State = (short)Visio.VisUIButtonState.visButtonDown;
                    break;

                case Global.MenuStateResetStatus:
                    nrtmMenuItemShowStatusItem.State = (short)Visio.VisUIButtonState.visButtonUp;
                    break;

                case Global.MenuStateSetReport:
                    nrtmMenuItemShowReportItem.State = (short)Visio.VisUIButtonState.visButtonDown;
                    break;

                case Global.MenuStateResetReport:
                    nrtmMenuItemShowReportItem.State = (short)Visio.VisUIButtonState.visButtonUp;
                    break;

                case Global.MenuStateToggleStatus:
                    ToggleMenuItemState(nrtmMenuItemShowStatusItem);
                    break;

                case Global.MenuStateToggleReport:
                    ToggleMenuItemState(nrtmMenuItemShowReportItem);
                    break;

                default:
                    break;
            }
            uiObject.UpdateUI();
            return;
        }      

        /// <summary>
        /// Function to remove the NRTM monitor menu from Visio
        /// </summary>
        public void RemoveNRTMMenu()
        {
            ////Remove NRTM menu
            nrtmMenu.Delete();
            if (uiObject != null)
            {
                uiObject.UpdateUI();
            }
        }

        /// <summary>
        /// Toggle state of menu item
        /// </summary>
        /// <param name="menuItem"></param>
        internal void ToggleMenuItemState(Visio.MenuItem menuItem)
        {
            if (menuItem.State == (short)Visio.VisUIButtonState.visButtonDown)
            {
                menuItem.State = (short)Visio.VisUIButtonState.visButtonUp;
            }
            else
            {
                menuItem.State = (short)Visio.VisUIButtonState.visButtonDown;
            }
            //Refresh menu object
            uiObject.UpdateUI();
        }

        /// <summary>
        /// Toggle window visibility
        /// </summary>
        /// <param name="currWindow"></param>
        internal static void ToggleWindowState(AddonWindowWrapper currWindow)
        {
            // toggle the state of this button
            if (currWindow.Visible == true)
            {
                currWindow.Visible = false;
            }
            else
            {
                currWindow.Visible = true;
            }
        }

        #endregion
    }
}
